package com.example.demo_job.service.impl;


import com.example.demo_job.entity.Position;
import com.example.demo_job.entity.PositionRescruit;
import com.example.demo_job.mapper.PositionMapper;
import com.example.demo_job.service.PositionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Service
public class PositionServiceimpl implements PositionService {
    @Autowired
    private PositionMapper positionMapper;
    @Override
    public List<PositionRescruit> ShowPosition(String cusername) {
        List<Position> PositionList = positionMapper.ShowPosition(cusername);
        List<PositionRescruit> positionRescruitList=new ArrayList<PositionRescruit>();
        for(Position position:PositionList){
            int pid=position.getPid();
            int applynum=positionMapper.findapplynumBypid(pid);
            PositionRescruit positionRescruit=new PositionRescruit(position,applynum);
            positionRescruitList.add(positionRescruit);
        }
        return positionRescruitList;
    }

    @Override
    public String FindInfo(int pid) {
        return positionMapper.FindInfo(pid);
    }

    @Override
    public void DeletePosition(int pid) {
        positionMapper.DeletePosition(pid);
        positionMapper.DeleteRecruit(pid);
    }

    @Override
    public void InsertPosition(int cid,String pname,String pinfo,double salary,String paddress,int enumber) {
        positionMapper.InsertPosition(cid, pname, pinfo, salary, paddress,enumber);
    }

    @Override
    public String FindPinfo(int pid) {
        String info=positionMapper.findPinfo(pid);
        if(info==null) return null;
//        String Tinfo="C:\\Users\\22263\\Desktop\\大学相关\\java\\idea\\demo_job\\src\\main\\resources\\static\\PositionTxt\\"+info;
        String Tinfo="src\\main\\resources\\static\\PositionTxt\\"+info;
        return Tinfo;
    }

    @Override
    public void Write(String tsinfo, String cv) {
        //tsinfo绝对路径，cv文本内容
        // 创建文件输出流
        BufferedWriter writer = null;
        try {
            writer = new BufferedWriter(new FileWriter(tsinfo));
        } catch (IOException e) {
            e.printStackTrace();
        }

        // 将文本数据写入文件
        try {
            writer.write(cv);
        } catch (IOException e) {
            e.printStackTrace();
        }

        // 关闭文件输出流
        try {
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public Position FindPositionBypid(int pid) {
        return positionMapper.findPositionBypid(pid);
    }

    @Override
    public void UpdatePosition(int pid,String pname,double salary,String paddress,int enumber) {
        positionMapper.updatePosition(pid,pname,salary,paddress,enumber);
    }

    @Override
    public String findPnameBypid(int pid) {
        return positionMapper.findPnameBypid(pid);
    }

    @Override
    public int findPidByPnameAndCid(int cid1, String pname) {
        return positionMapper.findPidByPnameAndCid(cid1,pname);
    }

    @Override
    public List<PositionRescruit> searchPosition(int cid, String value) {
        List<Position> positionList=positionMapper.searchPosition(cid,value);
        List<PositionRescruit> positionRescruitList=new ArrayList<PositionRescruit>();
        //搜索不到直接返回
        if(positionList.size()==0) return positionRescruitList;
        for (Position position:positionList){
            int pid=position.getPid();
            int applynum=positionMapper.findapplynumBypid(pid);
            PositionRescruit positionRescruit=new PositionRescruit(position,applynum);
            positionRescruitList.add(positionRescruit);
        }
        return positionRescruitList;
    }
}
